package xyz.jpcr.covid19app.Fragment;

import android.content.Context;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import xyz.jpcr.covid19app.Adapter.CollectionAdapter;
import xyz.jpcr.covid19app.R;
import xyz.jpcr.covid19app.Util.IDBHelper;

public class CollectionFragment extends Fragment {
    SwipeRefreshLayout swipeRefreshLayout;
    RecyclerView recyclerView;
    Context context;
    CollectionAdapter collectionAdapter;
    IDBHelper idbHelper;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_collection, container, false);
        context = getContext();
        idbHelper = new IDBHelper(context);
        swipeRefreshLayout = view.findViewById(R.id.fragment_collection_swipeRefreshLayout);
        recyclerView = view.findViewById(R.id.fragment_collection_recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        collectionAdapter = new CollectionAdapter(context, idbHelper.getAll());
        recyclerView.setAdapter(collectionAdapter);

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                collectionAdapter = new CollectionAdapter(context, idbHelper.getAll());
                recyclerView.setAdapter(collectionAdapter);
                swipeRefreshLayout.setRefreshing(false);
            }
        });
        return view;
    }
}